/**
 * @class 伴星实体
 * @author DuskyHuang 圣子
*/

import { _decorator, Component, Color, Node, renderer, MeshRenderer } from 'cc';
import { attenuate } from 'algorithms';
import { cosmos } from 'proxies';
const { ccclass, property } = _decorator;

@ccclass('cocosmos.star-companion')
export class StarCompanion extends Component {

    @property(Node) public prominence: Node = null;

    private _proPass0: renderer.Pass = null;
    private _proHandle: handle = 0;
    private _base10: pow10;

    protected onLoad(): void {
        this._proPass0 = this.prominence.getComponent(MeshRenderer).material.passes[0];
        this._proHandle = this._proPass0.getHandle('mainColor');
    }

    public setParams(base10: pow10, avg: Color): void {
        this._base10 = base10;
        this.getComponent(MeshRenderer).material.setProperty('mainColor', avg);
        prom.set(avg);
    }

    protected update(): void {
        const { pow10 } = cosmos;
        const alpha = attenuate(pow10, [this._base10 + 1, this._base10 + 2, -3, -1], false);
        this.prominence.setWorldScale(0.35, 0.35, 0.35);
        prom.a = alpha * 255;
        this._proPass0.setUniform(this._proHandle, prom);
    }
}

const prom = new Color;